package com.sojson.util.properties;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.yaml.snakeyaml.Yaml;

import lombok.extern.slf4j.Slf4j;

/**
 * yml文件工具类
 * 
 * @author liu
 * @date 2023-03-09
 */
@Slf4j
public class YmlUtil {

    /**
     * 读取resources下所有相同名字的properties文件，返回一个Properites对象
     * 
     * @param fileName
     * @return
     * @throws IOException
     */
    public static Properties getProperties(String fileName) throws IOException {
        return getProperties(new ClassPathResource(fileName));
    }

    /**
     * 根据yml文件路径获取Properties对象
     * 
     * @param resource
     * @return 得到的Properties对象
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    public static Properties getProperties(Resource resource) throws IOException {
        Properties properties = new Properties();

        InputStream inputStream = null;
        try {
            inputStream = resource.getInputStream();
            Yaml props = new Yaml();
            Object obj = props.loadAs(inputStream, Map.class);
            Map<String, Object> param = (Map<String, Object>)obj;

            for (Map.Entry<String, Object> entry : param.entrySet()) {
                String key = entry.getKey();
                Object val = entry.getValue();
                if (val instanceof Map<?, ?>) {
                    forEachYaml(properties, key, (Map<String, Object>)val);
                } else {
                    properties.put(key, val.toString());
                }
            }
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }

        return properties;
    }

    /**
     * 循环读取属性文件
     * 
     * @param properties
     * @param key
     * @param vals
     * @return
     */
    @SuppressWarnings("unchecked")
    private static void forEachYaml(Properties properties, String key, Map<String, Object> vals) {
        Set<Entry<String, Object>> entrySet = vals.entrySet();
        for (Entry<String, Object> entry : entrySet) {
            Object keyStr = entry.getKey();
            Object val = entry.getValue();
            // 新Key
            String strNew = "";
            if (StringUtils.isNotEmpty(key)) {
                strNew = key + "." + keyStr;
            } else {
                strNew = key;
            }

            if (val instanceof Map<?, ?>) {
                forEachYaml(properties, strNew, (Map<String, Object>)val);
            } else {
                properties.put(strNew, val.toString());
            }
        }
    }

}